<!--
 * @Author: zhuhaohua 250293786@qq.com
 * @Date: 2024-01-02 10:21:51
 * @LastEditors: DeluxePig 250293786@qq.com
 * @LastEditTime: 2024-06-30 22:42:19
 * @FilePath: \heart-backed\src\views\banner\index.vue
 * @Description: 任务中心
-->
<template>
  <div class="task-center-container">
    <div class="contral-box">
      <div class="con-list">
        <el-input v-model="contralData.searchName" class="z-input" placeholder="请输入模型编号" />
      </div>
      <el-button type="primary" @click="seachFun()">查询</el-button>
    </div>
    <div class="table-con">
      <div class="table-containe">
        <BaseTable :columns="column" :table-data="tableData">
          <template #model_base_info="{ row }">
            {{ row.model_base_info.title }}
          </template>
          <template #progress_status="{ row }">
            {{ column[2].formatter(row.progress_status) }}
          </template>
          <template #nlp_video_size="{ row }">
            {{ bytesTosize(row.nlp_video_size) }}
          </template>
          <template #nlp_video_duration="{ row }">
            {{ row.nlp_video_duration }}s
          </template>
          <template #do="{ row }">
            <el-button type="text" size="small" @click="resetFun(row)">
              重启任务
            </el-button>
          </template>
        </BaseTable>
      </div>
      <Pagination :pagination-data="pageData" :page-sizes="[10, 15, 20, 25, 30]" @paginationFun="changePage" />
    </div>
  </div>
</template>

<script>
import { getTaskApi, renewTaskApi } from '@/api/task-center'
import BaseTable from '@/components/bmp/table/index.vue'
import Pagination from '@/components/pagination/pagination'
export default {
  components: {
    BaseTable,
    Pagination
  },

  data() {
    return {
      contralData: {
        searchName: ''
      },
      column: [
        {
          prop: 'model_uniqueid',
          label: '模型ID'
        },
        {
          prop: 'model_base_info',
          label: '模型名称'
        },
        {
          prop: 'progress_status',
          label: '状态',
          formatter: function (data) {
            let text = '等待创建tts生成任务'
            switch (data) {
              case 0: text = '等待创建tts生成任务'; break
              case 5000: text = '确认tts生成结果'; break
              case 10000: text = '等待创建视频任务'; break
              case 20000: text = '已上报'; break
              case 30000: text = '生成完毕(开始下载)'; break
              case 40000: text = '下载完毕'; break
              case 50000: text = '已上传os'; break
              case 60000: text = '已经完成'; break
            }
            return text
          }
        },
        {
          prop: 'create_datetime',
          label: '任务创建时间'
        },
        {
          prop: 'update_datetime',
          label: '任务更新时间'
        },
        {
          prop: 'nlp_video_size',
          label: '消耗空间'
        },
        {
          prop: 'nlp_video_duration',
          label: '消耗视频时长'
        },
        {
          prop: 'do',
          label: '操作'
        }
      ],
      tableData: [
        {
          'id': 9,
          'users_uniqueid': 'n6mbo00lqb0d0djwg4dx5bnuc3npmgqr',
          'video_generate_actor_uid': '01YiREwt',
          'video_generate_voice_uid': '6ZRFBODk',
          'video_generate_layout_background_type': 'color',
          'video_generate_layout_background_background': '#F2D612',
          'video_generate_layout_avatar_x': '10',
          'video_generate_layout_avatar_y': '20',
          'video_generate_layout_avatar_width': '540',
          'video_generate_layout_avatar_height': '970',
          'type': 103000, // 任务类型:101000=模型兜底视频任务,102000=模型欢迎视频任务,103000=faq答案视频任务,103001=faq答案视频更新任务
          'model_uniqueid': '8414ae599c104189ae99c64d19d54dbe', // 任务所属模型
          'media_hash': 'ae663384d4764219be07a79d77c29162', // 媒体资源值
          'tts_text': '你好,遗憾的告知您刚才的提问我不知道答案,但已经将该问题反馈给作者,请静待作者添加该问题答案.谢谢您的使用!', // 所用tts文本
          'progress_status': 60000, // 执行进度:0=等待创建tts生成任务,5000=确认tts生成结果,10000=等待创建视频任务,20000=已上报,30000=生成完毕(开始下载),40000=下载完毕,50000=已上传oss,60000=已经完成
          'cancel_switch': 0,
          'oss_video_url': 'n6mbo00lqb0d0djwg4dx5bnuc3npmgqr/8414ae599c104189ae99c64d19d54dbe/welcome/d2df16b689db4542bab98e2fbb6f3ed1.mp4', // oss存放路径
          'nlp_tts_url': 'http://api.cyrinn.cn/api/mio/tmp/audio/20240514/075235/979df542.wav',
          'nlp_tts_task_id': 'c84d7cdd-c2f6-4403-92f6-4211691c930e',
          'nlp_video_task_id': '206a8c0c-b070-4be0-8583-e01aca074c7a',
          'nlp_video_url': 'http://api.cyrinn.cn/api/mio/api/videos/CPkMHQGX/20240514/155242.63f5a85f/full.mp4',
          'nlp_video_duration': '14', // 视频时长(秒)
          'nlp_video_size': '6490686', // 视频大小(字节)
          'fail_switch': 0, // 异常终止:0=否,1=是
          'fail_message': '', // 终止原因
          'faq_uniqueid': '5f21be85850a4dd696e95c166f21da63', // faq唯一值,当任务类型为103000与103001时存在
          'create_datetime': '2024-04-17 16:00:49',
          'update_datetime': '2024-05-14 15:53:09',
          'model_base_info': {
            'title': ''
          }// 模型基础信息
        }
      ],
      pageData: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      }
    }
  },
  created() {
    this.getTableData()
  },
  methods: {
    seachFun() {
      // console.log(this.contralData)
      this.getTableData()
    },
    bytesTosize(bytes) {
      if (bytes === 0 || bytes === '') {
        return {
          size: 0,
          unit: 'B'
        }
      }
      const k = 1024
      const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
      const i = Math.floor(Math.log(bytes) / Math.log(k))
      return (bytes / Math.pow(k, i)).toFixed(2) + ' ' + sizes[i]
      // return {
      //   size: (bytes / Math.pow(k, i)).toFixed(2),
      //   unit: sizes[i]
      // }
    },
    getTableData() {
      const data = {
        model_uniqueid: this.contralData.searchName,
        page_num: this.pageData.currentPage,
        limit_size: this.pageData.pageSize
      }
      getTaskApi(data).then((res) => {
        this.tableData = res.data.data
        this.pageData.total = res.data.total
      })
    },
    changePage(val) {
      this.pageData.currentPage = val.currentPage
      this.pageData.pageSize = val.pageSize
      this.getTableData()
    },
    resetFun(data) {
      if (data.type === 103000 || data.type === 103001) {
        this.$router.push({ path: '/model-info/reserve-task', query: { model_uniqueid: data.model_uniqueid, faq_uniqueid: data.faq_uniqueid } })
      } else {
        this.resetFun2(data.id)
      }
    },
    resetFun2(id) {
      this.$confirm('确认重启该任务?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const data = {
          id: id
        }
        renewTaskApi(data).then((res) => {
          this.$message({
            message: '该任务已重启！',
            type: 'success'
          })
          this.getTableData()
        }).catch(() => {
          this.$message({
            message: '重启失败，请重试！',
            type: 'warning'
          })
        })
      }).catch(() => {
        //
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.task-center-container {
  width: 100%;
  padding: 20px;
  height: 100%;
  background-color: #fff;

  .contral-box {
    width: 100%;
    height: 32px;
    font-weight: 400;
    font-size: 14px;
    color: #9E9E9E;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    margin: 10px 0;
    padding-right: 10px;

    .con-list {
      width: 240px;
      height: 100%;
      border-radius: 3px 3px 3px 3px;
      border: 1px solid #E0E0E0;
      margin-right: 12px;

    }

    .con-list2 {
      width: 360px;
    }

    .el-button {
      padding: 9px 20px;
    }
  }

  .table-con {
    width: 100%;
    height: calc(100% - 40px);

    .table-containe {
      width: 100%;
      height: calc(100% - 40px);
      margin-bottom: 10px;
    }

  }
}
</style>
